const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getShopData(data);
				break;
			case 'queryOne':
				res.data = await getTodoData(data);
				break;
			case 'queryTwo':
				res.data = await getGoodsData(data);
				break;
			case 'queryThree':
				res.data = await getOrderData(data);
				break;
			case 'queryFour':
				res.data = await getUserData(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}
/**
 * 获取店铺数据
 */
async function getShopData(data) {
	var result = await db.collection('setting').get();
	return result.data;
}
/**
 * 获取待处理数据
 */
async function getTodoData(data) {
	//待付款总数
	var result1 = await db.collection('order').where({
		'state': 1
	}).count();
	//待发货总数
	var result2 = await db.collection('order').where({
		'state': 2
	}).count();
	//待提货总数
	var result3 = await db.collection('order').where({
		'state': 4
	}).count();
	return {
		count1: result1.total,
		count2: result2.total,
		count3: result3.total
	}
}
/**
 * 获取订单数据
 */
async function getOrderData(data) {
	let nowDate = new Date(new Date().toLocaleDateString()).getTime();
	//今天成交订单数
	var r1 = await db.collection('order').where({
		state: dbCmd.gte(1),
		timestampCT: dbCmd.gte(nowDate)
	}).count();
	//总成交订单数
	var r2 = await db.collection('order').where({
		state: dbCmd.gte(1)
	}).count();
	//今天成交额
	var r3 = await db.collection('order').where({
		state: dbCmd.gte(1),
		timestampCT: dbCmd.gte(nowDate)
	}).get();
	let todayTotalPrice = 0;
	for(var i in r3.data) {
	    todayTotalPrice = todayTotalPrice+r3.data[i].totalPrice
	};
	//总成交额
	var r4 = await db.collection('order').where({
		state: dbCmd.gte(1)
	}).get();
	let totalPrice = 0;
	for(var j in r4.data) {
	    totalPrice = totalPrice+r4.data[j].totalPrice
	};
	return {
		count1: r1.total,
		count2: r2.total,
		count3: todayTotalPrice,
		count4: totalPrice
	}
}
/**
 * 获取商品数据
 */
async function getGoodsData(data) {
	var result1 = await db.collection('goods')
		.where({
			proData: {
				showState: '上架'
			}
		})
		.count()
	var result2 = await db.collection('goods')
		.where({
			proData: {
				showState: '下架'
			}
		})
		.count()
	return {
		count1: result1.total,
		count2: result2.total
	}
}
/**
 * 获取用户数据
 */
async function getUserData(data) {
	let nowDate = new Date(new Date().toLocaleDateString()).getTime();
	//今天的登录用户数
	var r1 = await db.collection('uni-id-users').where({
		username: dbCmd.neq('admin'),
		last_login_date: dbCmd.gte(nowDate)
	}).count();
	
	//所有用户数
	var r2 = await db.collection('uni-id-users').where({
		username: dbCmd.neq('admin')
	}).count();
	
	//今天下订单用户数
	var r3 = await db.collection('uni-id-users').where({
		username: dbCmd.neq('admin'),
		last_buy_date: dbCmd.gte(nowDate)
	}).count();
	
	//所有下订单用户数
	var r4 = await db.collection('uni-id-users').where({
		username: dbCmd.neq('admin'),
		last_buy_date: dbCmd.neq('null')
	}).count();
	
	return {
		count1: r1.total,
		count2: r2.total,
		count3: r3.total,
		count4: r4.total
	}
}